<?php

namespace app\common\model;

use think\Model;


class News extends Model
{

    

    

    // 表名
    protected $name = 'news';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'comment_nums',
        'like_nums',
    ];


    public function getCommentNumsAttr($value,$data)
    {
        return NewsComment::where([
            'news_id' => $data['id'],
        ])->count();
    }

    public function getLikeNumsAttr($value,$data)
    {
        return NewsLike::where([
            'news_id' => $data['id'],
        ])->count();
    }



    public function teacher()
    {
        return $this->belongsTo('app\common\model\Teacher', 'teacher_id', 'user_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function cate()
    {
        return $this->belongsTo('app\common\model\NewsCate', 'news_cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }





}
